package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.AlarmPushDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.SolarEnergyDto;
import com.hualude.roadsign.model.HighLeverEntity;
import com.hualude.roadsign.model.SolarEnergyEntity;
import com.hualude.roadsign.repository.AlarmPushRepository;
import com.hualude.roadsign.repository.AlarmRepository;
import com.hualude.roadsign.repository.SolarEnergyRepository;
import com.hualude.roadsign.repository.SolarFaultRepository;
import com.hualude.roadsign.service.SolarEnergyManageService;
import com.hualude.roadsign.utils.FormatNumber;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by shao-yikun on 2017/7/11.
 */
@Service
public class SolarEnergyManageServiceImpl implements SolarEnergyManageService {
    @Resource
    private SolarEnergyRepository solarEnergyRepository;
    @Resource
    private SolarFaultRepository solarFaultRepository;

    @Resource
    private AlarmRepository alarmRepository;

    @Resource
    private AlarmPushRepository alarmPushRepository;

    @Override
    public DTResponseDto getSolarEnergy4DT(DTRequestDto dtRequestDto,
                                           String actorIds,
                                           Integer orgId,
                                           Boolean audit,
                                           String startRoad,
                                           String endRoad,
                                           String roadName,
                                           Integer alarmBinding) throws Exception {
        List<SolarEnergyDto> solarEnergyDtos = solarEnergyRepository.getSolarEnergy4DT(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName, alarmBinding);
        Integer count = solarEnergyRepository.getSolarEnergy4DTCount(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName, alarmBinding);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, solarEnergyDtos);
    }

    @Override
    public List<SolarEnergyDto> getSolarEnergy4APP(
            String actorIds, Integer orgId, Integer mCount, Integer mSize,
            String city, String startRoad, String endRoad, String roadName, String alarmType) throws Exception {
        return solarEnergyRepository.getSolarEnergy4APP(actorIds, orgId, mCount, mSize, city, startRoad, endRoad, roadName, alarmType);
    }

    @Override
    @Transactional
    public Integer addSolarEnergy(SolarEnergyDto solarEnergyDto) throws Exception {
        solarEnergyDto.setSolarEnergyIsComplete(0);
        solarEnergyDto.setSolarEnergyLongitude(FormatNumber.resize(solarEnergyDto.getSolarEnergyLongitude()));
        solarEnergyDto.setSolarEnergyLatitude(FormatNumber.resize(solarEnergyDto.getSolarEnergyLatitude()));

        Integer devicePathId = solarEnergyRepository.save(solarEnergyDto.toBean()).getSolarEnergyId();
        String deviceId = solarEnergyDto.getSolarEnergyADeviceId();
        if (!deviceId.isEmpty()) {
            alarmRepository.updapteAlarmPath(2, devicePathId, deviceId);
        }
        return devicePathId;
    }

    @Override
    @Transactional
    public Integer deleteSolarEnergy(String solarEnergyIds) throws Exception {
        String[] solarEnergyIdArray = solarEnergyIds.split(",");
        for (String solarEnergyId : solarEnergyIdArray) {
            solarFaultRepository.deleteSolarFault(Integer.parseInt(solarEnergyId));
            solarEnergyRepository.deleteSolarEnergy(Integer.parseInt(solarEnergyId));
        }
        return solarEnergyIdArray.length;
    }

    @Override
    @Transactional
    public Integer updateSolarEnergy(SolarEnergyDto solarEnergyDto) throws Exception {
        return solarEnergyRepository.updateSolarEnergy(
                solarEnergyDto.getSolarEnergyId(),
                solarEnergyDto.getProjectNumber(),
                solarEnergyDto.getSolarEnergyLongitude(),
                solarEnergyDto.getSolarEnergyLatitude(),
                solarEnergyDto.getSolarEnergyProvince(),
                solarEnergyDto.getSolarEnergyCity(),
                solarEnergyDto.getSolarEnergyArea(),
                solarEnergyDto.getSolarEnergyRoadName(),
                solarEnergyDto.getSolarEnergyStartRoad(),
                solarEnergyDto.getSolarEnergyEndRoad(),
                solarEnergyDto.getSolarEnergyType(),
                solarEnergyDto.getSolarEnergyStyle(),
                solarEnergyDto.getSolarEnergyMaterial(),
                solarEnergyDto.getSolarEnergyBrand(),
                solarEnergyDto.getSolarEnergyPhoto(),
                solarEnergyDto.getSolarEnergyInstallTime(),
                solarEnergyDto.getSolarEnergyInstallOrg(),
                solarEnergyDto.getSolarEnergyPeriod(),
                solarEnergyDto.getSolarEnergyServiceLife(),
                solarEnergyDto.getSolarEnergyRemark(),
                solarEnergyDto.getSolarEnergyCreatorId(),
                solarEnergyDto.getSolarEnergyModifierId(),
                solarEnergyDto.getSolarEnergyOrgId(),
                solarEnergyDto.getSolarEnergyMainOrgId());
    }

    @Override
    @Transactional
    public Integer auditSolarEnergy(String solarEnergyIds) throws Exception {
        String[] solarEnergyIdArray = solarEnergyIds.split(",");
        for (String solarEnergyId : solarEnergyIdArray) {
            solarEnergyRepository.auditSolarEnergy(Integer.parseInt(solarEnergyId));
        }
        return solarEnergyIdArray.length;
    }

    @Override
    public List<String> getRoadByRoadName(String roadName, Integer actorId, Integer orgId) throws Exception {
        return solarEnergyRepository.getRoadByRoadName(roadName, actorId, orgId);
    }

    @Override
    public List<String> getSectionByRoadName(String roadName, Integer actorId, Integer orgId) throws Exception {
        return solarEnergyRepository.getSectionByRoadName(roadName, actorId, orgId);
    }

    @Override
    public SolarEnergyDto getSolarEnergyById(Integer solarEnergyId) throws Exception {
        return solarEnergyRepository.getSolarEnergyById(solarEnergyId).get(0);
    }

    @Override
    public List<String> getCity4SolarEnergy4App(Integer actorId, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return solarEnergyRepository.getCity4SolarEnergy4App(actorId, orgId, mCount, mSize);
    }

    @Override
    public List<String> getRoad4SolarEnergy(Integer actorId, Integer orgId, String city, Integer mCount, Integer mSize) throws Exception {
        return solarEnergyRepository.getRoad4SolarEnergy(actorId, orgId, city, mCount, mSize);

    }

    @Override
    public List<String> getSectionByRoadName4App(String roadName, String solarEnergyRoadName, String city, Integer actorId, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return solarEnergyRepository.getSectionByRoadName4App(roadName, solarEnergyRoadName, city, actorId, orgId, mCount, mSize);

    }

    @Override
    public List<String> getStartRoad4App(String roadName, String city, Integer actorId, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return solarEnergyRepository.getStartRoad4App(roadName, city, actorId, orgId, mCount, mSize);
    }

    @Override
    public List<SolarEnergyDto> getSolarEnergy4Map4App(Integer orgId,
                                                       String actorIds,
                                                       double neLongitude,
                                                       double neLatitude,
                                                       double swLongitude,
                                                       double swLatitude) throws Exception {
        return solarEnergyRepository.getSolarEnergy4Map4App(orgId, actorIds, neLongitude, neLatitude, swLongitude, swLatitude);
    }

    /**
     * @Description:
     * @Author: WYX.
     * @Date: 2018/5/1 19:00.
     */
    @Override
    @Transactional
    public Integer bindingDevice(String deviceId, Integer solarEnergyId) throws Exception {
        SolarEnergyEntity entity = solarEnergyRepository.findOne(solarEnergyId);
        StringBuffer position = new StringBuffer();
        position.append(entity.getSolarEnergyProvince());
        position.append(entity.getSolarEnergyCity());
        position.append(entity.getSolarEnergyArea());
        position.append(" ");
        position.append(entity.getSolarEnergyRoadName());
        position.append(" ");
        position.append(entity.getSolarEnergyStartRoad());
        position.append("-");
        position.append(entity.getSolarEnergyEndRoad());
        int count1 = solarEnergyRepository.bindingDevice(deviceId, solarEnergyId);
        count1 += alarmRepository.bindingDevice(4, solarEnergyId, position.toString(), deviceId);
        return count1;
    }

    @Override
    @Transactional
    public Integer unbindingDevice(String deviceId, Integer solarEnergyId) throws Exception {
        int count = solarEnergyRepository.unbindingDevice(solarEnergyId);
        count += alarmRepository.unbindingDevice(deviceId);
        return count;

    }

    @Override
    public DTResponseDto showAlarmHistory(DTRequestDto dtRequestDto, String deviceId, Integer deviceType) throws Exception {
        List<AlarmPushDto> dtos = alarmPushRepository.getAlarmHistory4DT(dtRequestDto, deviceId, deviceType);
        int count = alarmPushRepository.getAlarmHistoryCount4DT(dtRequestDto, deviceId, deviceType);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, dtos);
    }

    @Override
    public List<SolarEnergyDto> getDeviceInfo(Integer deviceId) throws Exception {
        return solarEnergyRepository.getDeviceInfo(deviceId);
    }


}
